package com.dslu.fx.controller;

import com.dslu.fx.dao.StockBillDao;
import com.dslu.fx.domain.AreaDO;
import com.dslu.fx.service.DzpService;
import com.dslu.fx.utils.CheckEmptyUtil;
import com.dslu.fx.utils.monologfx.MonologFXUtil;
import com.dslu.fx.vo.ProdVO;
import de.felixroske.jfxsupport.FXMLController;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import onbon.y2.Y2Exception;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@FXMLController
public class SomeButtonsController {

    @FXML
    private TableView<ProdVO> tableView;

    @FXML
    private TextField text_1;

    @Autowired
    private StockBillDao stockBillDao;

    @Autowired
    private DzpService dzpService;


    public void reactOnClick(final Event e) throws Y2Exception {
        String text = text_1.getText();

        if(CheckEmptyUtil.isEmpty(text)){
           // new Alert(Alert.AlertType.INFORMATION, "请输入库区名称", new ButtonType[]{ButtonType.CLOSE}).show();
            MonologFXUtil.alertError("库区名称不能为空，请输入库区名称!");
            return;
        }

        AreaDO areaDO= dzpService.getAreaByName(text);

        if(areaDO==null){
            MonologFXUtil.alertError("此库区不存在，请确认后再输入");
            return;
        }

        tableView.getColumns().clear();

        List<ProdVO> prodVOs =dzpService.flushDzpByAreaId(areaDO.getId());

        // List<ProdVO> prodVOs = stockBillDao.getStockByAreaId(40);

        //ObservableList<ProdVO> ls2= FXCollections.observableArrayList();

           ObservableList<ProdVO> ls2=FXCollections.observableArrayList();
            prodVOs.forEach(w->{
                ls2.add(w);
            });

            tableView.setItems(ls2);//将集合的值 存储到tableView里

        TableColumn<ProdVO, String> prodName=new TableColumn<ProdVO, String>("装备名称");//创建TableColumn  列名为序号
        TableColumn<ProdVO, String> specifications=new TableColumn<ProdVO, String>("规格");
        TableColumn<ProdVO, Number> num=new TableColumn<ProdVO, Number>("数量");
        TableColumn<ProdVO, String> emit=new TableColumn<ProdVO, String>("单位");

        prodName.setCellValueFactory(new PropertyValueFactory<ProdVO, String>("prodName"));
        specifications.setCellValueFactory(new PropertyValueFactory<ProdVO, String>("specifications"));
        num.setCellValueFactory(new PropertyValueFactory<ProdVO, Number>("num"));
        emit.setCellValueFactory(new PropertyValueFactory<ProdVO, String>("emit"));

        tableView.getColumns().add(prodName);
        tableView.getColumns().add(specifications);
        tableView.getColumns().add(num);
        tableView.getColumns().add(emit);

    }
}
